package com.example.service;

import org.springframework.stereotype.Service;

import com.example.entity.message.Message;
import com.example.util.EncodeUtil;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 将将要发送的信息编码
 * 
 * @author yao
 *
 */
@Service
public class MessageEncoder extends MessageToByteEncoder<Message> {

	@Override
	protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("编码开始");
		ByteBuf buf = EncodeUtil.encode(msg);
		System.out.println(ByteBufUtil.hexDump(buf));
		out.writeBytes(buf);
	}

}
